package org.qiyu.live.api.service.impl;

import org.apache.dubbo.config.annotation.DubboReference;
import org.qiyu.live.api.service.ILivingRoomService;
import org.qiyu.live.api.vo.LivingRoomInitVO;
import org.qiyu.live.api.vo.red.LivingRoomRedVo;
import org.qiyu.live.api.vo.resp.LivingRoomPageRespVO;
import org.qiyu.live.api.vo.resp.LivingRoomRespVO;
import org.qiyu.live.common.interfaces.dto.PageWrapper;
import org.qiyu.live.common.interfaces.utils.ConvertBeanUtils;
import org.qiyu.live.living.interfaces.dto.LivingRoomReqDTO;
import org.qiyu.live.living.interfaces.dto.LivingRoomRespDTO;
import org.qiyu.live.living.interfaces.rpc.ILivingRoomRpc;
import org.qiyu.live.user.dto.UserDTO;
import org.qiyu.live.user.interfaces.IUserRpc;
import org.qiyu.live.web.starter.QiyuRequestContext;
import org.springframework.stereotype.Service;

/**
 * @Description: //todo
 * @Author: 张广焱
 * @Date: 2023-12-13 21:29
 */
@Service
public class ILivingRoomServiceImpl implements ILivingRoomService {
    @DubboReference
    private ILivingRoomRpc iLivingRoomRpc;
    @DubboReference
    private IUserRpc iUserRpc;

    @Override
    public Integer startingLiving(Integer type) {
        Long userId = QiyuRequestContext.getUserId();
        UserDTO byUserId = iUserRpc.getByUserId(userId);
        LivingRoomReqDTO livingRoomReqDTO = new LivingRoomReqDTO();
        livingRoomReqDTO.setAnchorId(userId);
        livingRoomReqDTO.setRoomName("主播-" + QiyuRequestContext.getUserId() + "的直播间");
        livingRoomReqDTO.setCovertImg(byUserId.getAvatar());
        livingRoomReqDTO.setType(type);
        return iLivingRoomRpc.startLivingRoom(livingRoomReqDTO);
    }

    @Override
    public boolean closeLiving(Integer roomId) {
        LivingRoomReqDTO livingRoomReqDTO = new LivingRoomReqDTO();
        livingRoomReqDTO.setRoomId(roomId);
        livingRoomReqDTO.setAnchorId(QiyuRequestContext.getUserId());
        return iLivingRoomRpc.closeLiving(livingRoomReqDTO);
    }

    @Override
    public LivingRoomInitVO anchorConfig(Long userId, Integer roomId) {
        LivingRoomRespDTO reqDTO = iLivingRoomRpc.queryByRoomId(roomId);
        LivingRoomInitVO respVO = new LivingRoomInitVO();
        if (reqDTO == null || reqDTO.getAnchorId() == null || userId == null) {
            respVO.setRoomId(-1);
        } else {
            respVO.setRoomId(reqDTO.getId());
            respVO.setAnchorId(reqDTO.getAnchorId());
            respVO.setAnchor(reqDTO.getAnchorId().equals(userId));
        }
        return respVO;
    }

    @Override
    public LivingRoomPageRespVO  list(LivingRoomRedVo livingroomRedVo) {
        PageWrapper<LivingRoomRespDTO>  resultPage = iLivingRoomRpc.list(ConvertBeanUtils.convert(livingroomRedVo,LivingRoomReqDTO.class));
        LivingRoomPageRespVO livingRoomPageRespVO = new LivingRoomPageRespVO();
        livingRoomPageRespVO.setLsit(ConvertBeanUtils.convertList(resultPage.getList(), LivingRoomRespVO.class));
        livingRoomPageRespVO.setHasNext(resultPage.isHasNext());
        return livingRoomPageRespVO;
    }
}
